package DAO;


import Entidad.Soundtrack;
import javax.persistence.EntityManager;
import javax.persistence.Query;

public class DAO_Soundtrack {
    
    public String create(Soundtrack sound, EntityManager em) {
        em.getTransaction().begin();
        try {
            em.persist(sound);
            em.getTransaction().commit();
            return "Succes";
        } catch (Exception e) {
            System.out.println(e);
            em.getTransaction().rollback();
            return "Fail";
        }
    }
    
    public String delete(String picture, EntityManager em){
        em.getTransaction().begin();
        try{
            Query q = em.createQuery("DELETE FROM Song u WHERE u.picture = '" + picture + "'");
            q.executeUpdate();
            em.getTransaction().commit();
            return "Succes";
        }catch(Exception e){
            System.out.println(e);
            em.getTransaction().rollback();
            return "Fail";
        }
    }
    
    public void update(Soundtrack antiguo, Soundtrack nuevo, EntityManager em){
        em.getTransaction().begin();
        try{
            Query q = em.createQuery("UPDATE Soundtrack s SET s.picture = '" +
                    nuevo.getPicture()+"' WHERE s.nombre = '" + antiguo.getPicture() + "'");
            q.executeUpdate();
            em.getTransaction().commit();
        }catch(Exception e){
            System.out.println(e);
            em.getTransaction().rollback();
        }
    }
}
